home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / src / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-16  |  8.3 KB  |  309 lines

  1. /*      FILE.C
  2.  *
  3.  * High-level file I/O for MIDAS Sound System
  4.  *
  5.  * $Id: file.c,v 1.2 1997/01/16 18:41:59 pekangas Exp $
  6.  *
  7.  * Copyright 1996,1997 Housemarque Inc.
  8.  *
  9.  * This file is part of the MIDAS Sound System, and may only be
  10.  * used, modified and distributed under the terms of the MIDAS
  11.  * Sound System license, LICENSE.TXT. By continuing to use,
  12.  * modify or distribute this file you indicate that you have
  13.  * read the license and understand and accept it fully.
  14. */
  15.  
  16. /****************************************************************************\
  17. * Note! This module basicly calls directly the raw file I/O functions.
  18. * This kind of separation, however, is useful as it allows other kinds
  19. * of file systems, such as a compressed file library, to be added by
  20. * simply relinking MIDAS with another high-level file I/O module.
  21. \****************************************************************************/
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include "lang.h"
  28. #include "mtypes.h"
  29. #include "errors.h"
  30. #include "mmem.h"
  31. #include "rawfile.h"
  32. #include "file.h"
  33.  
  34. RCSID(const char *file_rcsid = "$Id: file.c,v 1.2 1997/01/16 18:41:59 pekangas Exp $";)
  35.  
  36.  
  37. /****************************************************************************\
  38. *
  39. * Function:     int fileOpen(char *fileName, int openMode, fileHandle *file);
  40. *
  41. * Description:  Opens a file for reading or writing
  42. *
  43. * Input:        char *fileName          name of file
  44. *               int openMode            file opening mode, see enum rfOpenMode
  45. *               fileHandle *file        pointer to file handle
  46. *
  47. * Returns:      MIDAS error code.
  48. *               File handle is stored in *file.
  49. *
  50. \****************************************************************************/
  51.  
  52. int CALLING fileOpen(char *fileName, int openMode, fileHandle *file)
  53. {
  54.     int         error;
  55.     int         rfMode;
  56.     fileHandle  hdl;
  57.  
  58.     /* allocate file structure: */
  59.     if ( (error = memAlloc(sizeof(fileFile), (void**) &hdl)) != OK )
  60.         PASSERROR(ID_fileOpen)
  61.  
  62.     switch ( openMode )
  63.     {
  64.         case fileOpenRead:      /* open file for reading */
  65.             rfMode = rfOpenRead;
  66.             break;
  67.  
  68.         case fileOpenWrite:     /* open file for writing */
  69.             rfMode = rfOpenWrite;
  70.             break;
  71.  
  72.         case fileOpenReadWrite: /* open file for reading and writing */
  73.             rfMode = rfOpenReadWrite;
  74.             break;
  75.  
  76.         default:
  77.             ERROR(ID_fileOpen, errInvalidArguments);
  78.             return errInvalidArguments;
  79.     }
  80.  
  81.     /* open file: */
  82.     if ( (error = rfOpen(fileName, rfMode, (rfHandle*) &hdl->rf)) != OK )
  83.         PASSERROR(ID_fileOpen)
  84.  
  85.     *file = hdl;
  86.  
  87.     return OK;
  88. }
  89.  
  90.  
  91.  
  92.  
  93. /****************************************************************************\
  94. *
  95. * Function:     int fileClose(fileHandle file);
  96. *
  97. * Description:  Closes a file opened with fileOpen().
  98. *
  99. * Input:        fileHandle file         handle of an open file
  100. *
  101. * Returns:      MIDAS error code
  102. *
  103. \****************************************************************************/
  104.  
  105. int CALLING fileClose(fileHandle file)
  106. {
  107.     int         error;
  108.  
  109.     /* close file: */
  110.     if ( (error = rfClose(file->rf)) != OK )
  111.         PASSERROR(ID_fileClose)
  112.  
  113.     /* deallocate file structure: */
  114.     if ( (error = memFree(file)) != OK )
  115.         PASSERROR(ID_fileClose)
  116.  
  117.     return OK;
  118. }
  119.  
  120.  
  121.  
  122.  
  123. /****************************************************************************\
  124. *
  125. * Function:     int fileGetSize(fileHandle file, long *fileSize);
  126. *
  127. * Description:  Get the size of a file
  128. *
  129. * Input:        fileHandle file         handle of an open file
  130. *               ulong *fileSize         pointer to file size
  131. *
  132. * Returns:      MIDAS error code.
  133. *               File size is stored in *fileSize.
  134. *
  135. \****************************************************************************/
  136.  
  137. int CALLING fileGetSize(fileHandle file, long *fileSize)
  138. {
  139.     int         error;
  140.  
  141.     /* get file size to *fileSize: */
  142.     if ( (error = rfGetSize(file->rf, fileSize)) != OK )
  143.         PASSERROR(ID_fileGetSize)
  144.  
  145.     return OK;
  146. }
  147.  
  148.  
  149.  
  150.  
  151. /****************************************************************************\
  152. *
  153. * Function:     int fileRead(fileHandle file, void *buffer, ulong numBytes);
  154. *
  155. * Description:  Reads binary data from a file
  156. *
  157. * Input:        fileHandle file         file handle
  158. *               void *buffer            reading buffer
  159. *               ulong numBytes          number of bytes to read
  160. *
  161. * Returns:      MIDAS error code.
  162. *               Read data is stored in *buffer, which must be large enough
  163. *               for it.
  164. *
  165. \****************************************************************************/
  166.  
  167. int CALLING fileRead(fileHandle file, void *buffer, ulong numBytes)
  168. {
  169.     int         error;
  170.  
  171.     /* read data from file: */
  172.     if ( (error = rfRead(file->rf, buffer, numBytes)) != OK )
  173.         PASSERROR(ID_fileRead)
  174.  
  175.     return OK;
  176. }
  177.  
  178.  
  179.  
  180.  
  181. /****************************************************************************\
  182. *
  183. * Function:     int fileWrite(fileHandle file, void *buffer, ulong numBytes);
  184. *
  185. * Description:  Writes binary data to a file
  186. *
  187. * Input:        fileHandle file         file handle
  188. *               void *buffer            pointer to data to be written
  189. *               ulong numBytes          number of bytes to write
  190. *
  191. * Returns:      MIDAS error code
  192. *
  193. \****************************************************************************/
  194.  
  195. int CALLING fileWrite(fileHandle file, void *buffer, ulong numBytes)
  196. {
  197.     int         error;
  198.  
  199.     /* write data to file: */
  200.     if ( (error = rfWrite(file->rf, buffer, numBytes)) != OK )
  201.         PASSERROR(ID_fileWrite)
  202.  
  203.     return OK;
  204. }
  205.  
  206.  
  207.  
  208.  
  209. /****************************************************************************\
  210. *
  211. * Function:     int fileSeek(fileHandle file, long newPosition, int seekMode);
  212. *
  213. * Description:  Seeks to a new position in file. Subsequent reads and writes
  214. *               go to the new position.
  215. *
  216. * Input:        fileHandle file         file handle
  217. *               long newPosition        new file position
  218. *               int seekMode            file seek mode, see enum rfSeekMode
  219. *
  220. * Returns:      MIDAS error code
  221. *
  222. \****************************************************************************/
  223.  
  224. int CALLING fileSeek(fileHandle file, long newPosition, int seekMode)
  225. {
  226.     int         error;
  227.     int         rfMode;
  228.  
  229.     switch ( seekMode )
  230.     {
  231.         case fileSeekAbsolute:      /* seek to an absolute position */
  232.             rfMode = rfSeekAbsolute;
  233.             break;
  234.  
  235.         case fileSeekRelative:      /* seek relative to current position */
  236.             rfMode = rfSeekRelative;
  237.             break;
  238.  
  239.         case fileSeekEnd:           /* seek from end of file */
  240.             rfMode = rfSeekEnd;
  241.             break;
  242.  
  243.         default:
  244.             ERROR(ID_fileSeek, errInvalidArguments);
  245.             return errInvalidArguments;
  246.     }
  247.  
  248.     /* seek to new file position: */
  249.     if ( (error = rfSeek(file->rf, newPosition, rfMode)) != OK )
  250.         PASSERROR(ID_fileSeek)
  251.  
  252.     return OK;
  253. }
  254.  
  255.  
  256.  
  257.  
  258. /****************************************************************************\
  259. *
  260. * Function:     int fileGetPosition(fileHandle file, long *position);
  261. *
  262. * Description:  Reads the current position in a file
  263. *
  264. * Input:        fileHandle file         file handle
  265. *               long *position          pointer to file position
  266. *
  267. * Returns:      MIDAS error code.
  268. *               Current file position is stored in *position.
  269. *
  270. \****************************************************************************/
  271.  
  272. int CALLING fileGetPosition(fileHandle file, long *position)
  273. {
  274.     int         error;
  275.  
  276.     /* get current file position to *position: */
  277.     if ( (error = rfGetPosition(file->rf, position)) != OK )
  278.         PASSERROR(ID_fileGetPosition)
  279.  
  280.     return OK;
  281. }
  282.  
  283.  
  284.  
  285.  
  286. /****************************************************************************\
  287. *
  288. * Function:     int fileExists(char *fileName, int *exists);
  289. *
  290. * Description:  Checks if a file exists or not
  291. *
  292. * Input:        char *fileName          file name, ASCIIZ
  293. *               int *exists             pointer to file exists status
  294. *
  295. * Returns:      MIDAS error code.
  296. *               *exists contains 1 if file exists, 0 if not.
  297. *
  298. \****************************************************************************/
  299.  
  300. int CALLING fileExists(char *fileName, int *exists)
  301. {
  302.     int         error;
  303.  
  304.     if ( (error = rfFileExists(fileName, exists)) != OK )
  305.         PASSERROR(ID_fileExists);
  306.  
  307.     return OK;
  308. }
  309.